Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
float number conversions with ".toString" is funcional in JavaScript?

For example, if I convert number 1.1 to binary (or any other number base) is it possible to return it to decimal float number?

console.log((1.1).toString(2));
console.log((1.1).toString(8));
console.log((1.1).toString(16));

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

JS has a few built in methods for parsing numbers. Using parseFloat here would be ideal, however unfortunately parseFloat does not allow you to specify the radix (base) under which the string should be parsed.

However, the parseInt function does let you specify the radix, but it can only work with integer numbers and will fail when trying to parse a decimal. But with a little bit of math you can parse the whole and decimal parts of your string separately, then recombine them to get the converted number.

Perhaps something like:

console.log((1.1).toString(2));
console.log((1.1).toString(8));
console.log((1.1).toString(16));

function parseInBase(num_str, base) {
  const [integer_part, decimal_part] = num_str.split(".");
  const integer_part_num = parseInt(integer_part, base);
  
  if (!decimal_part) return integer_part_num;
  
  // Trim trailing zeroes:
  const sanitized_dec_part = decimal_part.replace(/0+$/, "");
  const decimal_part_num = parseInt(sanitized_dec_part, base);
  
  return integer_part_num + decimal_part_num / base**sanitized_dec_part.length;
}

console.log(parseInBase((1.1).toString(2), 2));
console.log(parseInBase((1.1).toString(8), 8));
console.log(parseInBase((1.1).toString(16), 16));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda